<?php
include '../../../wp-blog-header.php';
$brand_id = '528';

$url = 'http://www.fishthedeal.com/xmlposts/buzzrposts.xml';
function download_page($path){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$path);
        curl_setopt($ch, CURLOPT_FAILONERROR,1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 15);
        $retValue = curl_exec($ch);                      
        curl_close($ch);
        return $retValue;
}

$curl_json_output = download_page($url);
$ftd_deals = xml2array($curl_json_output);

$ftd_daily_deals = $ftd_deals['offers'];

//echo "<pre>";
//print_r($ftd_daily_deals);
//exit();

foreach($ftd_daily_deals as $key => $value)
{
$columns_deal['name'] = pods_sanitize($value['offer_title']);
$columns_deal['camp_link'] = sanitize_title($columns_deal['name']);
$ext_img_url = $value['offer_image_url'];
 
$deal = new Pod('deal');
$where = 't.camp_link = "'.$columns_deal['camp_link'].'" OR (t.ext_image_url = "'.$ext_img_url.'" AND t.daily = "1")';
$deal->findRecords('id DESC', 1,$where);
$dealsFound = $deal->getTotalRows();
//echo $dealsFound;
if($dealsFound == 1)
{
  while($deal->fetchRecord())
  {
    $deal_id = $deal->get_field('id');
    $camp_end = strtotime($deal->get_field('camp_end'));
    $offer_end = strtotime($value['offer_end_date']);
    //echo $camp_end;
    //echo $offer_end;
    if($offer_end > $camp_end)
    {
      $dealsFound = 0;
    }
  }
}

if($dealsFound == 0)
{
  if($value['offer_cities'] == 'All'){
    unset($ids);
    unset($current_cities);
    $City = new Pod('city');
  	$City->findRecords('name ASC', -1);
  	while($City->fetchRecord())
  	{
    	$ids  = $City->get_field('id');
    	$current_cities[] = $ids;
  	}
    $current_cities = implode(',',$current_cities);
  }
  else{
    $city_name = $value['offer_cities'];
    $city = new Pod('city');
  	$city->findRecords("name ASC",1,"t.name = '$city_name'");
  	while($city->fetchRecord())
  	{
    	$current_cities  = $city->get_field('id');
  	}
  }
  
  $columns_deal['city'] = $current_cities;
  
  $shortened_url = make_bitly_url($value['offer_url']);
  if(empty($shortened_url))
  {
    $shortened_url = make_goog_url($value['offer_url']);
  }
  $columns_deal['extlink'] = $shortened_url;
  
  $columns_deal['camp_end'] = $value['offer_end_date'];
  $columns_deal['camp_start'] = $value['offer_start_date'];
  $columns_deal['brand'] = $brand_id;
  $columns_deal['daily'] = '1';
  $columns_deal['oriprice'] = floor($value['offer_original_price']);
  $columns_deal['disper'] = floor(((($value['offer_original_price'])-($value['offer_discounted_price']))/($value['offer_original_price']))*100);
  $columns_deal['disprice'] = floor($value['offer_discounted_price']);
  $columns_deal['ext_image_url'] = $ext_img_url;

  /*
  $Category = new Pod('category');
  $cat = $value['offer_category'];
	$Category->findRecords("id ASC",-1,"t.name='$cat'");
	while($Category->fetchRecord())
	{
  	$cat_id  = $Category->get_field('id');
	}
  $columns_deal['camp_cat'] = $cat_id;
  */
  $cat_id = check_daily_category($columns_deal['name']);
  if(!empty($cat_id))
  {
    $columns_deal['camp_cat'] = $cat_id;
  }

  $api = new PodAPI();
  if(isset($deal_id) && !empty($deal_id))
  {
    $params = array('datatype' => 'deal','tbl_row_id'=>$deal_id, 'columns' => $columns_deal);
  }
  else
  {
    $params = array('datatype' => 'deal', 'columns' => $columns_deal);
  }
  
  /*
  echo '<pre>';
  print_r($params);
  echo '</pre>';
  exit();
  */
  
  $api->save_pod_item($params);
}

}